package com.eleventwelve.iris.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.opentides.util.StringUtil;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.eleventwelve.iris.entity.PatientMedicalProgram;
import com.eleventwelve.iris.service.PatientMedicalProgramService;

/**
 * 
 * @author Ashley
 *
 */
public class PatientMedicalProgramDetailsController extends AbstractController{
	private String viewName;
	private String preview;
	private PatientMedicalProgramService patientMedicalProgramService;
	
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Map<String, Object> map = new HashMap<String, Object>();
		PatientMedicalProgram program = null;
		String viewType="";
		
		String programId = request.getParameter("id");
		if(!StringUtil.isEmpty(programId) && StringUtils.isNumeric(programId)){
			program = patientMedicalProgramService.load(programId);
		}
		
		if(program!=null){
			map.put("program", program);
			
			String previewParam = request.getParameter("peak");
			
			if(previewParam!=null && previewParam.equalsIgnoreCase("true")){
				viewType = this.preview;
			} else {
				viewType = viewName;
			}
			
			return new ModelAndView(viewType, map);
		} else {
			return new ModelAndView(viewName);		//should be the error view
		}
	}


	public String getViewName() {
		return viewName;
	}


	public void setViewName(String viewName) {
		this.viewName = viewName;
	}


	public PatientMedicalProgramService getPatientMedicalProgramService() {
		return patientMedicalProgramService;
	}


	public void setPatientMedicalProgramService(
			PatientMedicalProgramService patientMedicalProgramService) {
		this.patientMedicalProgramService = patientMedicalProgramService;
	}


	public String getPreview() {
		return preview;
	}


	public void setPreview(String preview) {
		this.preview = preview;
	}

}
